//
//  CollectionView.swift 字符串,集合
//  swiftStudy
//
//  Created by Zhang on 2021/3/22.
//

import SwiftUI

struct CollectionView: View {
    
    let code_1: String
    let code_2: String
    let code_3: String
    
    init() {
        code_1 =
            "var str = \"Hello, playground\"\n" +
            "\n" +
            "//字符串还可以通过构造方法进行创建\n" +
            "str = String(describing: Int.self)\n" +
            "\n" +
            "//其他,通过构造函数进行数据的类型转换\n" +
            "var a = Int(1.05)\n" +
            "var b = Float(a)"
        
        code_2 =
            "var str_01 = \"hello\"\n" +
            "var str_02 = \",\"\n" +
            "var str_03 = \"world\"\n" +
            "var str_04 = str_01 + str_02 + str_03\n" +
            "\n" +
            "//\"\\()\"结构可以将其他数据类型转换为字符串进行插入\n" +
            "var int_01 = 111\n" +
            "var str_05 = \"\\(int_01)数字\""
        
        code_3 =
            "//创建一个字符\n" +
            "var c:Character = \"a\"\n" +
            "//创建一个字符数组\n" +
            "var cs:[Character] = [\"H\",\"E\",\"L\",\"L\",\"O\"]\n" +
            "\n" +
            "//打印字符数组\n" +
            "cs.forEach { c in\n" +
            "    print(c)\n" +
            "}\n" +
            "\n" +
            "//通过构造函数构造字符串\n" +
            "var str_06 = String(cs)"
    }
    
    
    var body: some View {
        ScrollView(.vertical){
            VStack(alignment: .leading, spacing: 5){
                
                Section(header: SectionHeaderView(title: "字符串")){
                    CommonTextView(content: "字符串类型为一串字符的组合,在Swift语言中,String类型实际上是一个结构体,Swift语言中的结构体十分强大,可以像类一样进行属性和方法定义,定义一个String类型的变量,如下:")
                    
                    CodeView(code: code_1).padding(10)
                }
                
                Section(header: SectionHeaderView(title: "字符串的组合")){
                    CommonTextView(content: "Swift中的String类型对'+'运算符进行了重载,可以使用'+'将多个字符串组合拼接")
                    
                    CodeView(code: code_2).padding(10)
                }
                
                //content 类似于kotlin中的高阶函数,如果放在参数的末尾可以直接在函数后面跟{}
                Section(header: SectionHeaderView(title: "字符类型"), content: {
                    CommonTextView(content: "字符类型用来表示单个的字符,Swift中使用Character来描述字符类型")
                    
                    CodeView(code: code_3).padding(10)
                })
                
            }
            //设置VStack,横向填充屏幕
            .frame(minWidth: 0,
                   maxWidth: .infinity,
                   minHeight: 0,
                   maxHeight: .infinity,
                   alignment: .topLeading
            )
        }
    }
}

struct CollectionView_Previews: PreviewProvider {
    static var previews: some View {
        CollectionView()
    }
}
